package com.example.rocketmq_demo.concumer;

import com.example.rocketmq_demo.entity.ScheduleDto;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OtherConsumer {
    public static void main(String[] args) throws MQClientException {
        ScheduleDto scheduleDto = new ScheduleDto();


        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("one");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.subscribe("TopicOne", "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) { // 输出消息被消费的时间 1234567891011121314




                    System.out.print(new SimpleDateFormat("mm:ss").format(new Date()));
                    //System.out.println(" ," + msg.toString());
                    System.out.println(" ," + new String(msg.getBody()));
                } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        }); consumer.start();
        System.out.println("Consumer Started");
    }
}